<?php

/**
 * @author CHUN Dany <chundany@gmail.com>
 */
class Mod_Banner extends CI_Model
{ 
  public function getBanner($start, $num)
  {
    $this->db->select('b.*, c.'.field('name').' AS '.field('category').', z.'.field('name').' AS '.field('zone'))
        ->from(table('banner').' b')
        ->join(table('category').' c', 'b.'.field('categoryId').'= c.'.field('id'))
        ->join(table('bannerZone').' z', 'b.'.field('zoneId').'= z.'.field('id'))
        ->order_by('b.'.field('updatedAt'), 'DESC');

    $this->db->limit($num, $start);

    return $this->db->get();
  }
  
  public function getBannerNum()
  {
    $objDb = $this->getAllBanner();
    return $objDb->num_rows();
  }
  
  public function getAllBanner() {
    $this->db->select('b.*, c.'.field('name').' AS '.field('category').', z.'.field('name').' AS '.field('zone'))
        ->from(table('banner').' b')
        ->join(table('category').' c', 'b.'.field('categoryId').'= c.'.field('id'))
        ->join(table('bannerZone').' z', 'b.'.field('zoneId').'= z.'.field('id'))
        ->order_by('b.'.field('updatedAt'), 'DESC');

    return $this->db->get();
  }
  
  public function getBannerById($id)
  {
    $this->db->select('b.*, c.'.field('name').' AS '.field('category').', z.'.field('name').' AS '.field('zone'))
        ->from(table('banner').' b')
        ->join(table('category').' c', 'b.'.field('categoryId').'= c.'.field('id'))
        ->join(table('bannerZone').' z', 'b.'.field('zoneId').'= z.'.field('id'))
        ->where('b.'.field('id'), $id);

    $result = $this->db->get();

    return $result->row_array();
  }
  
  public function getCategoryList($id = NULL)
  {
    $this->db->select(field('id') . ', ' . field('name'))
        ->from(table('category'))
        ->where(field('isDisable'), 0);

    if(!empty($id))
      $this->db->where(field('id').' !=', $id);
    $this->db->order_by(field('name'), 'ASC');
    $result = $this->db->get();
    
    $return = array('' => '');
    if($result->num_rows() > 0){
      foreach($result->result_array() as $row){
        $return[$row[field('id')]] = $row[field('name')];
      }
    }

    return $return;
  }
  
  public function getZoneList($id = NULL)
  {
    $this->db->select(field('id') . ', ' . field('name'))
        ->from(table('bannerZone'));

    if(!empty($id))
      $this->db->where(field('id').' !=', $id);
    $result = $this->db->get();
    
    $return = array('' => '');
    if($result->num_rows() > 0){
      foreach($result->result_array() as $row){
        $return[$row[field('id')]] = $row[field('name')];
      }
    }

    return $return;
  }
  
  public function insert($image = NULL)
  {
    $disable  = $this->input->post('disable', false);
    $data = array(
      field('name')       => $this->input->post('name'), 
      field('url')        => $this->input->post('url'),
      field('zoneId')     => $this->input->post('zone'),
      field('order')      => $this->input->post('order'),
      field('publishedAt')=> dateFormat($this->input->post('publish'), 'Y-m-d'),
      field('categoryId') => $this->input->post('category'),
      field('description')=> $this->input->post('description'),
      field('image')      => $image,
      field('isDisable')  => ($disable == 1 ? 1 : 0)
    );
    
    $this->db->insert(table('banner'), $data); 
    return $this->db->affected_rows();
  }
  
  public function update($id, $image = NULL)
  {
    $disable  = $this->input->post('disable', false);
    $data = array(
      field('name')       => $this->input->post('name'), 
      field('url')        => $this->input->post('url'),
      field('zoneId')     => $this->input->post('zone'),
      field('order')      => $this->input->post('order'),
      field('publishedAt')=> dateFormat($this->input->post('publish'), 'Y-m-d'),
      field('categoryId') => $this->input->post('category'),
      field('description')=> $this->input->post('description'),
      field('image')      => $image,
      field('isDisable')  => ($disable == 1 ? 1 : 0)
    );
    $this->db->set(field('updatedAt'), 'NOW()', FALSE);
    
    if(empty($image)) unset($data[field('image')]);

    $this->db->where(field('id'),$id);
    $this->db->update(table('banner'), $data);

    return $this->db->affected_rows();
  }
  
  public function delete($id)
  {
    $this->db->set(field('deletedAt'), 'NOW()', FALSE);
    $this->db->where(field('id'), $id);
    $this->db->update(table('banner'));
    return $this->db->affected_rows();
  }
}